package com.kaiwukj.android.ufamily.mvp.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.kaiwukj.android.mcas.di.component.AppComponent;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.BaseSwipeBackActivity;
import com.kaiwukj.android.ufamily.di.component.DaggerReportComponent;
import com.kaiwukj.android.ufamily.di.module.ReportModule;
import com.kaiwukj.android.ufamily.mvp.contract.ReportContract;
import com.kaiwukj.android.ufamily.mvp.http.entity.requestbak.ReportRequest;
import com.kaiwukj.android.ufamily.mvp.http.entity.resultbak.ComplainType;
import com.kaiwukj.android.ufamily.mvp.http.entity.resultbak.ReportTypeResult;
import com.kaiwukj.android.ufamily.mvp.presenter.ReportPresenter;
import com.kaiwukj.android.ufamily.mvp.ui.adapter.TypeAdapter;
import com.qmuiteam.qmui.widget.QMUITopBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt.NoteReport;

/**
 * @author Charles
 * @date 2019/8/22.
 * description：Kaiwu
 */
@Route(path = NoteReport)
public class ReportActivity extends BaseSwipeBackActivity<ReportPresenter> implements ReportContract.View {

    @BindView(R.id.qtb_report)
    QMUITopBar qtbReport;
    @BindView(R.id.tv_report_name)
    TextView tvReportName;
    @BindView(R.id.tv_report_content)
    TextView tvReportContent;
    @BindView(R.id.rv_report_type)
    RecyclerView rvReportType;
    @BindView(R.id.tv_commit)
    TextView tvCommit;

    TypeAdapter reportTypeAdapter;

    int typeId;

    @Autowired
    int noteId;

    @Autowired
    String nickName;

    @Autowired
    String noteContent;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerReportComponent
                .builder()
                .appComponent(appComponent)
                .reportModule(new ReportModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected int getLayoutId() {
        ARouter.getInstance().inject(this);
        return R.layout.activity_report;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {

    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        tvCommit.setOnClickListener(view -> commit());
        qtbReport.setTitle("举报");
        qtbReport.addLeftBackImageButton().setOnClickListener(view -> finish());

        mPresenter.requestReportType();

        setSpan(tvReportName, "举报" + nickName, 2);
        setSpan(tvReportContent, nickName + ":" + noteContent, nickName.length() + 1);
    }

    void initRecyclerView(List<ReportTypeResult> data) {
        rvReportType.setLayoutManager(new GridLayoutManager(this, 3));
        List<ComplainType> datas = new ArrayList<>();
        for (ReportTypeResult reportTypeResult : data) {
            ComplainType complainType = new ComplainType();
            complainType.setId(reportTypeResult.getId());
            complainType.setTypeName(reportTypeResult.getDicValue());
            datas.add(complainType);
        }
        reportTypeAdapter = new TypeAdapter(this, R.layout.item_type, datas);
        rvReportType.setAdapter(reportTypeAdapter);
        reportTypeAdapter.setOnSelectedListener(id -> typeId = id);
    }

    void commit() {
        if (typeId == 0) {
            showMessage("请选择举报类型");
            return;
        }
        ReportRequest reportRequest = new ReportRequest();
        reportRequest.setNoteId(noteId);
        reportRequest.setReportTypeId(typeId);
        mPresenter.postReport(reportRequest);
    }

    @Override
    public Context getCtx() {
        return null;
    }

    @Override
    public void getReportType(List<ReportTypeResult> data) {
        initRecyclerView(data);
    }

    @Override
    public void reportSuccess() {

        showMessage("举报成功");

        new Handler().postDelayed(this::finish, 500);

    }

    private void setSpan(TextView view, String text, int pos) {
        SpannableString spannableString = new SpannableString(text);
        spannableString.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.app_color_theme)), pos, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        view.setText(spannableString);
    }

    /*@Override
    public void showMessage(@NonNull String message) {
        dialog = new QMUITipDialog.Builder(this).setTipWord(message).create();
        dialog.setTitle(message);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
        new Handler().postDelayed(() -> dialog.dismiss(), 800);
    }*/

    @Override
    public void post(Runnable runnable) {

    }
}
